home *** CD-ROM | disk | FTP | other *** search
Text File | 1993-04-09 | 7.0 KB | 172 lines | [TEXT/KEEN] |
- #$MFS_SuperReplace - replace all instances of a pattern of text in one
- #or more files, even if the pattern to find spans more than one line.
- # Use the "Set variables" button to at least set the text for the
- #"find" and "replace" variables - in the text for "find", use a single space to
- #separate the words, and also put two spaces wherever the string of text might
- #be broken to continue on the next line. More on this below. Some options are
- #available: document all changes to stdout, treat the "find" string as pure
- #text rather than a regular expression, ignore case while searching.
- #
- #Variables that you can set with "Set variables":
- #Variable values, meaning, examples
- #-------- ----------------------------------------------
- #find the string of text to find, eg find =MyFUnction
- #replace the string of text to replace what's found,
- # eg replace =MyFunction
- #document 0 means don't document the changes, non -0 means print list
- # of file names and lines where changes were made, to stdout
- # And, it prints the original version before the change.
- #regex non-0 means your "find" string is a regular expression, and you should
- # escape special characters as noted below. A 0 value means
- # your "find" string is intended as a pure text string and all
- # characters in the string should be interpreted literally.
- #ignorecase 0 means don't ignore the case when searching, non-0 means treat
- # 'a' and 'A' the same, etc.
- #-the default values are:
- # document =1 (document changes to stdout)
- # regex =1 (treat the "find" string as a regular expression)
- #ignorecase =0 (be sensitive to upper and lower case)
- #-don't put a space between the '=' and the value unless you want the space
- #to be part of the value. No quotes should be used around values, whether
- #string or number.
- #
- #PLEASE NOTE if your "find" string is a regular expression (regex =1),
- #then characters which have special meaning when interpreted within a
- #regular expression should be ESCAPED if you want to match the literal character
- #- these are \^$.[]|()*+? . For example, to find
- # "MyFunc(s.member) + a[0]" set the "find" variable to
- # find=MyFunc\(s\.member\) \+ a\[0\]
- #If your "find" string is not a regular expression (regex =0),
- #then escapes for special punctuation will be inserted for you, except for
- #the escaping backslash "\" itself. In addition, you will need to use a backslash
- #to match an escape sequence, such as "\t" for a tab, or "\b" for word boundary.
- #For example, the "MyFunc" find string with regex =0 would be
- # find =MyFunc(s.member) + a[0]
- #
- #If you're not familiar with regular expressions yet, set regex to 0 and just
- #search for pure text until you feel ready to give \^$.[]|()*+? \1\> etc a try.
- #
- #NOTE in your replace string, a '&' or any of '\1' through '\9' will retain
- #their special meaning (& == all of what was matched, \1 == first tagged group etc)
- #regardless of the value of '"regex", though it’s not possible to tag groups
- #unless you set regex=1.
- #
- #For finding purposes, each single space in the "find" string
- #is converted to the regular-expression equivalent of
- # "at least one space, tab, or return". If some of the white space is
- #entirely optional, use two spaces - this produces
- #the equivalent of "zero or more spaces, tabs, or returns" for each pair of spaces
- #in the find pattern. Seemingly backwards, I know, but most space is of the
- #"one or more" kind rather than the "zero or more" kind.
-
- #Typically use the "MFS selected files" option after selecting the files
- #for multi–file operations within the calling application.
-
- #This program progressively adds input lines to the end of the variable "multi",
- #does the replacement, and then subtracts lines from the beginning of "multi",
- #storing them in the array "out[]". At the end of an input file, the entire
- #file is rewritten from out[] and multi.
- #Rewriting files with hAWK has the following side-effects:
- #- the file creator will change to '????', though no resources will be
- # lost.
- #- THINK C, EnterAct, etc projects will not be aware of the changes, so you
- # should select the "Update From Disk" command later to bring your project
- # up to date.
- #- marker locations will be thrown off. If you placed the markers using
- # EnterAct's "Automark" command, you can replace them with the same
- # command fairly painlessly.
- #
- #Sequence:
- #--Modify the find string
- #--Create the "actual" program to run, by reading the "template" program
- # and inserting the (static) strings to search for and use for replacement.
- #--Execute the new program with a "hAWK" call.
- #Note this is done entirely for speed: a static regular expression such as
- # match($0, "struct")
- #is parsed only once, whereas a dynamic regular expression such as
- # find = "struct"
- # match($0, find)
- #is parsed each time the match (or sub or gsub) statement is executed.
- #
- #BUG, goofball whitespace such as an option-space or formfeed left to you as an exercise.
-
- # User’s Manual references:
- # «hAWK User’s Manual» «F Running hAWK programs»
- # «hAWK User’s Manual» «L 5 Regular expressions»
- # «hAWK User’s Manual» «M 5 Built-in string and file functions»
- # «hAWK User’s Manual» «K 4 Built-in variables»
- # «hAWK User’s Manual» «K 8 Arrays»
- # «hAWK User’s Manual» «N User-defined functions»
- # «hAWK User’s Manual» «P 3 The getline function»
- # «hAWK User’s Manual» «O 3 Output into files»
- # «hAWK User’s Manual» «Q The hAWK function»
-
-
- BEGIN { maxLines = split(find, a) + 1;
- if (document+0 != 0)
- {
- document = 1
- printf("Searched for\n\t%s\n", find);
- printf("and replaced with\n\t%s\n", replace);
- num_files = ARGC-1
- }
- if (regex+0 == 0)
- gsub(/([$^.[\]|()*+?])/,"\\\1", find);#escape special characters
- gsub(/ /, "[ \\t\\n]*", find);#replace two blanks by optional whitespace
- gsub(/([^[]) /, "\1[ \\t\\n]+", find);#replace one blank by at least one whitespace
-
- #Create specific replace program from MFS_SuperReplace.T
- templateFile = STDPATH "Drag_on Modules:hAWK programs:" "MFS_SuperReplace.T"
- actualFile = STDPATH "Drag_on Modules:hAWK programs:" "MFS_SuperReplace.A"
- while (getline < templateFile > 0)
- {
- if (match($0, /##MARK/))
- {
- ++tLines
- if (match($0, /##MARK find/))
- {
- $0 = "\t\twhile (match(multi, /" find "/) > 0)"
- }
- else if (match($0, /##MARK repToo/))
- {
- $0 = "\t\t\tsub(/" find "/, \"" replace "\", multi);"
- }
- else if (match($0, /##MARK maxLines/))
- sub(/maxLines/, maxLines)
- else
- {
- print "Unknown ##MARK:"
- print $0
- exit
- }
- }
- print > actualFile
- }
- if (tLines < 3)#template missing or damaged
- {
- print "The template file"
- print templateFile
- if (!tLines)
- print "is missing."
- else
- print "is damaged."
- exit
- }
- else
- {
- close(templateFile)
- close(actualFile)
- }
- #Execute the new specific program MFS_SuperReplace.A
- argv[x++] = "hAWK"
- argv[x++] = "-f" actualFile
- argv[x++] = "-v" "ignorecase=" ignorecase
- argv[x++] = "-v" "document=" document
- if (document != 0)
- argv[x++] = "-v" "num_files=" num_files
- argv[x++] = "--"
- for (i = 1; i < ARGC; ++i)
- argv[x++] = ARGV[i]
- hAWK(argv); #execution transfers to MFS_SuperReplace.A
- }
-